package com.leyou.personnel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.pojo.Result;
import com.leyou.personnel.data.dto.AdministratorsAuthorityDto;
import com.leyou.personnel.data.entity.AdministratorsAuthority;
import com.leyou.personnel.data.vo.TreeVo;
import com.leyou.personnel.mapper.AdministratorsAuthorityMapper;
import com.leyou.personnel.service.AdministratorsAuthorityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xhh
 * @since 2022-05-19
 */

@Service
public class AdministratorsAuthorityServiceImpl extends ServiceImpl<AdministratorsAuthorityMapper, AdministratorsAuthority> implements AdministratorsAuthorityService {
    @Autowired
    private AdministratorsAuthorityMapper authorityMapper;
    @Autowired
    private AdministratorsAuthorityService authorityService;
    @Override
    public  Result authorityList() {
        List<AdministratorsAuthorityDto> resultList=new ArrayList<>();
        Result<List<TreeVo>> result=new Result<>();

        List<AdministratorsAuthority> list = authorityService.list();
        List<AdministratorsAuthorityDto> authorityList = new ArrayList<>();

        for (AdministratorsAuthority authority : list) {
            AdministratorsAuthorityDto dto=new AdministratorsAuthorityDto();
            BeanUtils.copyProperties(authority,dto);
            authorityList.add(dto);
        }

        for (AdministratorsAuthorityDto authority1 : authorityList) {
            for (AdministratorsAuthorityDto authority2 : authorityList) {
                if(authority1.getAuthorityNumber().equals(authority2.getAuthorityFatherNumber())){
                    authority1.getAuthorityDtoList().add(authority2);
                }
            }

            if(authority1.getAuthorityFatherNumber()!=null && authority1.getAuthorityFatherNumber().equals("main")){
                resultList.add(authority1);
            }
        }
        if(resultList.size()>0){
            result.setMsg("成功");
            result.setCode("200");
            List<TreeVo> treeVoList = transformationResultData(resultList);
            result.setData(treeVoList);
            result.setResult(Result.RESULT_FLG.SUCCESS.toString());

        }else{
            result.setMsg("失败");
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.toString());
        }
        return result;
    }



    /*
    * {
            label: '一级 1',
            id:'123',
            children: [{
              label: '二级 1-1',
              id:'123',
              children: [{
                label: '三级 1-1-1',
                id:'123'
              }]
            }]
          }
    *
    * */
    //将数据转换成指定的对象类型
    public List<TreeVo> transformationResultData(List<AdministratorsAuthorityDto> resultList){
        List<TreeVo> treeVoListlist=new ArrayList<>();
        diGui(resultList,treeVoListlist);
        return treeVoListlist;
    }

    public void diGui(List<AdministratorsAuthorityDto> resultList,List<TreeVo> treeVoListlist){
        for (AdministratorsAuthorityDto dto : resultList) {
            TreeVo treeVo=new TreeVo();
            treeVo.setLabel(dto.getAuthorityName());
            treeVo.setId(dto.getAuthorityNumber());
            if(dto.getAuthorityDtoList().size()>0){
                diGui(dto.getAuthorityDtoList(),treeVo.getChildren());
            }
            treeVoListlist.add(treeVo);
        }
    }


}